<?php
/**
 * @Author: liuhongbin
 * @Email: 514660094@qq.com
 * @Date: 2019-02-12 16:15:10
 * @Description:
 * @LastEditors: liuhongbin
 * @LastEditTime: xxx
 */

namespace app\controller\adminapi\platform\douyin;

use app\controller\adminapi\platform\PlatformController;
use think\App;
use app\common\model\platform\Platform;
use hiduoke\services\FormBuilder as Form;

/**
 * Class WorkWechat 抖音
 * @package app\controller\adminapi\platform\douyin
 * User: liuhongbin
 * Date: 2022-07-17 17:25
 */
class Douyin extends PlatformController
{
    /**
     * @var Platform
     */
    public $model;

    public function __construct(App $app, Platform $model)
    {
        parent::__construct($app);
        $this->model = $model;
    }

    /**
     * 显示抖音列表
     * @Route("wechat", method="GET")
     * @return \think\Response
     */
    public function index()
    {
        //
        return $this->success("ok");
    }

    /**
     * 显示创建抖音表单页.
     * @Route("wechat/create", method="GET")
     * @return \think\Response
     */
    public function create()
    {
        return $this->getForm(0);
    }

    /**
     * 保存新建的抖音
     * @Route("wechat", method="POST")
     * @param \think\Request $request
     * @return \think\Response
     */
    public function save()
    {
        return $this->saveForm(0);
    }

    /**
     * 显示编辑抖音表单页.
     * @Route("wechat/id/edit", method="GET")
     * @param int $id
     * @return \think\Response
     */
    public function edit($id)
    {
        return $this->getForm($id);
    }

    private function getForm($id)
    {
        $rule = array();
        //创建表单
        $row = $this->model->where(['plaid' => $id])->findOrEmpty();
        if ($row->isEmpty()) {
            //新增
            $form = Form::createForm("/adminapi/platform/douyin/index")->setMethod("POST");
            $title = "新增抖音平台";
        } else {
            $form = Form::createForm("/adminapi/platform/douyin/index/" . $id)->setMethod("PUT");
            $title = "编辑抖音平台";
        }
        //新增表单
        $rule[] = Form::hidden('type', 4);
        $rule[] = Form::number('sort', '排序', $row->sort ?? '');
        $rule[] = Form::input('name', '应用名称', $row['name'] ?? '')->required();
        $rule[] = Form::dkInput('config_client_key', '应用Key', $row['config']['client_key'] ?? '', '在开放平台中-管理中心-应用管理-网站应用-详情')->required();
        $rule[] = Form::dkInput('config_client_secret', '应用Secret', $row['config']['client_secret'] ?? '', '在开放平台中-管理中心-应用管理-网站应用-详情')->required();
        $rule[] = Form::dkInput('config_domain', '授权域回调', $row['config']['domain'] ?? '', "在开发平台中-管理中心-应用管理-网站应用-详情-基础信息");
        $rule[] = Form::input('description', '描述', $row->description ?? '');
        $rule[] = Form::dkImage('logo', '图标', $row->logo ?? '')->appendToBody();
        $form->setTitle($title)->setRule($rule);
        return $this->success("", Form::getFormData($form));
    }

    private function saveForm($id)
    {
        $wallet = $this->userInfo['wallet'];
        if ($wallet['douyin'] <= $wallet['use_count']['douyin']) {
            return $this->error("创建次数不足");
        }
        $formData = getFormData();
        if (!$id) {
            $formData['uid'] = $this->userInfo['uid'];
        }
        $row = $this->model->findOrEmpty($id);
        $row->save($formData);
        return $this->success("提交成功", ['plaid' => $row->plaid]);
    }

    /**
     * 保存更新的抖音
     * @Route("wechat/id", method="PUT")
     * @param \think\Request $request
     * @param int $id
     * @return \think\Response
     */
    public function update($id)
    {
        return $this->saveForm($id);
    }

}
